package com.guoliang.ucenterService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guoliang.commonUtils.JwtUtils;
import com.guoliang.commonUtils.MD5;
import com.guoliang.service.exceptionHandler.MyException;
import com.guoliang.ucenterService.entity.UcenterMember;
import com.guoliang.ucenterService.entity.vo.RegisterVo;
import com.guoliang.ucenterService.mapper.UcenterMemberMapper;
import com.guoliang.ucenterService.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author leo
 * @since 2023-01-27
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    @Autowired
    RedisTemplate<String,String> redisTemplate;
    @Override
    public String login(UcenterMember member) {
        String mail = member.getMail();
        String password = member.getPassword();
        //判断是否为空
        if (StringUtils.isEmpty(mail)||StringUtils.isEmpty(password)){
            throw new MyException(20001,"登陆失败");
        }
        //判断邮箱账号是否存在
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("mail",mail);
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);
        if (ucenterMember==null){
            throw new MyException(20001,"邮箱账号不存在");
        }
        //判断密码
        if(!MD5.encrypt(password).equals(ucenterMember.getPassword())){
            throw new MyException(20001,"密码错误");
        }
        //判断用户是否被禁用
        if (ucenterMember.getIsDisabled()){
            throw new MyException(20001,"登陆失败");
        }
        return JwtUtils.getJwtToken(ucenterMember.getId(),ucenterMember.getNickname());
    }

    @Override
    public void register(RegisterVo registerVo) {
        String code = registerVo.getCode();
        String mail = registerVo.getMail();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        if (code==null||mail==null||nickname==null||password==null){
            throw new MyException(20001,"必要信息为空，注册失败");
        }
        String codeRedis = redisTemplate.opsForValue().get(mail);
        if (!code.equals(codeRedis)){
            throw new MyException(20001,"邮箱验证码错误，注册失败");
        }
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("mail",mail);
        Long count = baseMapper.selectCount(wrapper);
        if (count!=0){
            throw new MyException(20001,"该邮箱已被注册");
        }
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setMail(mail);
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setNickname(nickname);
        ucenterMember.setAvatar("https://edu-3255.oss-cn-hangzhou.aliyuncs.com/2023/01/27/14b9ff8bfb92424a85093f7fed7ad342avater.jpg");
        baseMapper.insert(ucenterMember);
    }

    @Override
    public UcenterMember getMemberByOpenId(String openid) {
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("openid",openid);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public int getRegisterNum(String day) {
        int num=baseMapper.getRegisterNum(day);
        return num;
    }
}
